package com.hethongviet.vending.pages;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.hethongviet.vending.AppConfig;
import com.hethongviet.vending.R;
import com.hethongviet.vending.Utils;

public class EditMacAddressActivity extends Activity
{
	public static final String EXTRA_MAC_ADDRESS = "extra_mac";
	private static final int MAX_NUMBER_IN_MAC_ADDRESS = 6;
	private static final int MAX_LENGTH_OF_NUMBER_IN_MAC_ADDRESS = 2;
	private EditText[] _EditTextMacs = null;

	private TextView _TextViewWrongFormat = null;

	private Button _ButtonAccept = null;
	private Button _ButtonCancel = null;

	public EditMacAddressActivity()
	{
		_EditTextMacs = new EditText[MAX_NUMBER_IN_MAC_ADDRESS];
	}

	@Override
	public void onAttachedToWindow()
	{
		if (AppConfig.USE_ATTACH_WINDOW_FOR_DISABLE_HOME_KEY)
		{
			this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
			super.onAttachedToWindow();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		/* Full screen */
		Utils.prepareWindow(this);

		setContentView(R.layout.page_edit_mac_address);

		String macAddress = getIntent().getStringExtra(EXTRA_MAC_ADDRESS);

		_TextViewWrongFormat = (TextView) findViewById(R.id.config_edit_wrong_format);

		String[] strings = null;
		if (macAddress != null)
		{
			Utils.debug("edit mac = " + macAddress);
			strings = macAddress.split("[:]");
			Utils.debug("split -> " + strings);
		}

		if (strings == null || strings.length != MAX_NUMBER_IN_MAC_ADDRESS)
		{
			strings = null;
		}

		for (int i = 0; i < MAX_NUMBER_IN_MAC_ADDRESS; i++)
		{
			_EditTextMacs[i] = (EditText) findViewById(R.id.config_edit_mac_number_1
					+ i);
			if (strings != null)
			{
				_EditTextMacs[i].setText(strings[i]);
			}

			// new
			// EditTextLocker(_EditTextMacs[i]).limitCharactersHex(MAX_LENGTH_OF_NUMBER_IN_MAC_ADDRESS);

			_EditTextMacs[i].addTextChangedListener(new TextWatcher()
			{

				@Override
				public void onTextChanged(CharSequence arg0, int arg1,
						int arg2, int arg3)
				{

				}

				@Override
				public void beforeTextChanged(CharSequence arg0, int arg1,
						int arg2, int arg3)
				{
					_TextViewWrongFormat.setVisibility(View.GONE);
				}

				@Override
				public void afterTextChanged(Editable arg0)
				{

				}
			});
		}

		_ButtonAccept = (Button) findViewById(R.id.button_config_edit_accept);
		_ButtonCancel = (Button) findViewById(R.id.button_config_edit_cancel);

		_ButtonAccept.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				String[] textNumbers = new String[MAX_NUMBER_IN_MAC_ADDRESS];

				boolean isValidText = true;

				for (int i = 0; i < MAX_NUMBER_IN_MAC_ADDRESS; i++)
				{
					textNumbers[i] = _EditTextMacs[i].getText().toString().toUpperCase();
				}
				for (int i = 0; i < MAX_NUMBER_IN_MAC_ADDRESS; i++)
				{
					if (textNumbers[i].length() != MAX_LENGTH_OF_NUMBER_IN_MAC_ADDRESS)
					{
						isValidText = false;
						break;
					}

					int lenTextI = textNumbers[i].length();
					for (int j = 0; j < lenTextI; j++)
					{
						char charLast = textNumbers[i].charAt(j);
						if (!(charLast == 'A' || charLast == 'B'
								|| charLast == 'C' || charLast == 'D'
								|| charLast == 'E' || charLast == 'F' || Character.isDigit(charLast)))
						{
							isValidText = false;
							break;
						}
					}
					if (!isValidText)
						break;
				}

				if (!isValidText)
				{
					_TextViewWrongFormat.setVisibility(View.VISIBLE);
				}
				else
				{
					_TextViewWrongFormat.setVisibility(View.GONE);

					String textMac = "";
					for (int i = 0; i < MAX_NUMBER_IN_MAC_ADDRESS; i++)
					{
						if (i == MAX_NUMBER_IN_MAC_ADDRESS - 1)
						{
							textMac += textNumbers[i];
						}
						else
							textMac += textNumbers[i] + ":";
					}

					Utils.debug("Mac address new = " + textMac);
					Intent intent = new Intent();
					intent.putExtra(EXTRA_MAC_ADDRESS, textMac);
					setResult(RESULT_OK, intent);
					finish();
				}
			}
		});

		_ButtonCancel.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				finish();
			}
		});

	}

}
